package com.cloudcc.mobile.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.mobile.util.photo.Util;
import com.cloudcc.mobile.weight.viewgroup.CheckImage;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * 多图选择
 * 
 * @author seven
 * 
 */
public class PhotoGridAdapter extends BaseAdapter {
	private ImageLoadingListener animateFirstListener = new Util.AnimateFirstDisplayListener();
	private List<String> mSelected = null;
	private List<String> mData = null;
	private OnMyItemListener mItemListener;
	private int selectSize;
	

	public PhotoGridAdapter(Context mContext,List<String> data,int selectLegth) {
		this.mData =data;
		this.selectSize = selectLegth;
		mSelected = new ArrayList<String>();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View view;
		


//		CheckImage image = vh.getView(R.id.photoview);
//		LogUtils.d("app", "当前获得的项目:"+path);
//
//		Constants.imageLoader.displayImage("file://" + path,
//				image.getImageView(), Constants.image_display_options,
//				animateFirstListener);
//		
//		
//		image.setChecked(isSelected(path));
//
//		image.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//			@Override
//			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
//				LogUtils.d("app", "触发点击事件："+isChecked);
//				select(path);
//			}
//		});

	
		
		
		
		return null;
	}

	/**
	 * 判断是否 选中
	 * 
	 * @param path
	 * @return
	 */
	private boolean isSelected(String path) {
		return mSelected.contains(path);
	}

	/**
	 * 选中
	 * 
	 * @param path
	 */
	private void select(String path) {
		if (mSelected.size() > selectSize) {
			if (mItemListener != null) {
				mItemListener.onSelectedSizeOutBound(mSelected.size());
			}
			return;
		}
		if (isSelected(path)) {// 如果存在就是不选中
			mSelected.remove(path);
			LogUtils.d("app", "删除:"+path);
		} else {
			mSelected.add(path);
			LogUtils.d("app", "新增:"+path);
		}
	}

	/**
	 * 设置监听
	 * 
	 * @param l
	 */
	public void setItemListener(OnMyItemListener l) {
		this.mItemListener = l;
	}

	/**
	 * 获得选中的
	 * 
	 * @return
	 */
	public List<String> getSelectedItem() {
		return mSelected;
	}

	public interface OnMyItemListener {
		/**
		 * 当选中的图片太多的时候
		 * 
		 * @param size
		 */
		void onSelectedSizeOutBound(int size);
	}

	@Override
	public int getCount() {
		return (mData==null||mData.size()==0)?0:mData.size();
	}

	@Override
	public String getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	
	private static class ViewHolder{
		public CheckImage image;
	}

}
